var API            = require('API');

module.exports = ['$scope', function ($scope) {
    $scope.filter_shop = '';
    $scope.revise_param = {};
    $scope.param = {};
    $scope.add_shop = true;
    $scope.add_shop_title = true;
    $scope.upd_shop_title = false;

    $scope.brands_selected_to_add = [];
    $scope.brands_selected_to_remove = [];
    $scope.shop_all = [];

    $scope.forms = {
        data:[],
        shop:{},
        desc:'describe',
        del:'delete'
    };
    $scope.$watch('forms.shop', function(item){
        $scope.revise_shop = true;
        $scope.add_shop = false;
        $scope.add_shop_title = false;
        $scope.upd_shop_title = true;
        $scope.revise_param = item;
    });

    //获取所有门店信息
    function refresh() {
        alert('asd');
            API.shop.all(function (data) {
                $scope.$apply(function () {
                    $scope.shop_all = data;
                    $scope.forms.data = $scope.data = data.map(function (o) {
                        return o;
                    });
                });
            });

    }

    refresh();

    //修改提交
    $scope.revise_submit = function () {
        var cur_shop = $scope.revise_param;
        cur_shop.brands = cur_shop._brands.map(function(b){ return "'" + b + "'"}).join(',');
        API.shop.upd(cur_shop, function (data) {
            alert('保存成功');
            //refresh();
        });
    };

    $scope.add_shop_ = function () {
        $scope.revise_shop = false;
        $scope.add_shop = true;
        $scope.add_shop_title = true;
        $scope.upd_shop_title = false;
    };

    //添加
    $scope.delete = function () {
        if (confirm('确定要删除门店' + $scope.revise_param.name)) {
            API.shop.del($scope.revise_param, function (data) {
                refresh();
            });
        }
    };
    //添加
    $scope.submit = function () {
        API.shop.add($scope.param, function (data) {
            refresh();
        });
    }

    // $scope.first = true;

    // $scope.is_last = function(index){
    //     if(index == $scope.shop_all.length - 1){
    //         if($scope.first){
    //             $scope.first = false;
    //             $(".shops_shapeshift").shapeshift();
    //         }
    //     }
    // };
    // is_last();

}];
